# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

cases:
  # layer list
  - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql layer ls
    expected: expected/layer.yml
  # service list
  - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql service layer GENERAL
    expected: expected/service.yml
  # service instance list
  - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql instance list --service-name=e2e-service-provider
    expected: expected/service-instance-provider.yml
  - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql instance list --service-name=e2e-service-consumer
    expected: expected/service-instance-consumer.yml
  # service endpoint
  - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql endpoint list --keyword=users --service-name=e2e-service-provider
    expected: expected/service-endpoint-provider.yml
  - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql endpoint list --keyword=users --service-name=e2e-service-consumer
    expected: expected/service-endpoint-consumer.yml
  # dependency service
  - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql dependency service --service-name=e2e-service-provider
    expected: expected/dependency-services-provider.yml
  - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql dependency service --service-name=e2e-service-consumer
    expected: expected/dependency-services-consumer.yml
  # dependency instance
  - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql dependency instance --service-name=e2e-service-consumer --dest-service-name=e2e-service-provider
    expected: expected/dependency-instance.yml
  # dependency endpoint
  - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql dependency endpoint --service-name=e2e-service-provider --endpoint-name=POST:/users
    expected: expected/dependency-endpoint-provider.yml
  - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql dependency endpoint --service-name=e2e-service-consumer --endpoint-name=POST:/users
    expected: expected/dependency-endpoint-consumer.yml
  # trace segment list
  - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql trace ls
    expected: expected/traces-list.yml
  # negative tags search: relationship should be logical AND instead of logical OR
  - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql trace ls --tags http.method=POST,http.status_code=201
    expected: expected/empty-traces-list.yml
  # trace detail
  - query: |
      swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql trace $( \
        swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql trace ls \
          | yq e '.traces | select(.[].endpointnames[0]=="POST:/users") | .[0].traceids[0]' -
      )
    expected: expected/trace-users-detail.yml
  # service metrics
  - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics single --name=service_sla --service-name=e2e-service-provider
    expected: expected/metrics-single-sla.yml
  - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics nullable --name=service_sla --service-name=e2e-service-provider
    expected: expected/metrics-nullable-single-sla.yml
  - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics nullable --name=service_sla --service-name=e2e-service-provider --start "2023-01-08" --end "2023-01-09"
    expected: expected/metrics-nullable-single-sla-empty.yml
  - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics sorted --name service_apdex 5
    expected: expected/metrics-top-service.yml
  - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics top --name service_sla 5
    expected: expected/metrics-top-service.yml
  - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics exec --expression=service_sla --service-name=e2e-service-provider
    expected: expected/metrics-has-value.yml
  - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics exec --expression=service_cpm --service-name=e2e-service-provider
    expected: expected/metrics-has-value.yml
  - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics exec --expression=service_resp_time --service-name=e2e-service-provider
    expected: expected/metrics-has-value.yml
  - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics exec --expression=service_apdex --service-name=e2e-service-provider
    expected: expected/metrics-has-value.yml
  - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics exec --expression=service_sla --service-name=e2e-service-consumer
    expected: expected/metrics-has-value.yml
  - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics exec --expression=service_cpm --service-name=e2e-service-consumer
    expected: expected/metrics-has-value.yml
  - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics exec --expression=service_resp_time --service-name=e2e-service-consumer
    expected: expected/metrics-has-value.yml
  - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics exec --expression=service_apdex --service-name=e2e-service-consumer
    expected: expected/metrics-has-value.yml
  # service instance metrics
  - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics exec --expression=service_instance_resp_time --instance-name=consumer1 --service-name=e2e-service-consumer
    expected: expected/metrics-has-value.yml
  - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics exec --expression=service_instance_cpm --instance-name=consumer1 --service-name=e2e-service-consumer
    expected: expected/metrics-has-value.yml
  - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics exec --expression=service_instance_sla --instance-name=consumer1 --service-name=e2e-service-consumer
    expected: expected/metrics-has-value.yml
  # service instance JVM metrics
  - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics exec --expression=instance_jvm_memory_heap --instance-name=consumer1 --service-name=e2e-service-consumer
    expected: expected/metrics-has-value.yml
  - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics exec --expression=instance_jvm_memory_heap_max --instance-name=consumer1 --service-name=e2e-service-consumer
    expected: expected/metrics-has-value.yml
  - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics exec --expression=instance_jvm_memory_noheap --instance-name=consumer1 --service-name=e2e-service-consumer
    expected: expected/metrics-has-value.yml
  - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics exec --expression=instance_jvm_thread_live_count --instance-name=consumer1 --service-name=e2e-service-consumer
    expected: expected/metrics-has-value.yml
  - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics exec --expression=instance_jvm_thread_daemon_count --instance-name=consumer1 --service-name=e2e-service-consumer
    expected: expected/metrics-has-value.yml
  - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics exec --expression=instance_jvm_thread_peak_count --instance-name=consumer1 --service-name=e2e-service-consumer
    expected: expected/metrics-has-value.yml
  - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics exec --expression=instance_jvm_thread_runnable_state_thread_count --instance-name=consumer1 --service-name=e2e-service-consumer
    expected: expected/metrics-has-value.yml
  - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics exec --expression=instance_jvm_class_loaded_class_count --instance-name=consumer1 --service-name=e2e-service-consumer
    expected: expected/metrics-has-value.yml
  - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics exec --expression=instance_jvm_class_total_loaded_class_count --instance-name=consumer1 --service-name=e2e-service-consumer
    expected: expected/metrics-has-value.yml
  - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics exec --expression=instance_jvm_memory_heap --instance-name=provider1 --service-name=e2e-service-provider
    expected: expected/metrics-has-value.yml
  - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics exec --expression=instance_jvm_memory_heap_max --instance-name=provider1 --service-name=e2e-service-provider
    expected: expected/metrics-has-value.yml
  - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics exec --expression=instance_jvm_memory_noheap --instance-name=provider1 --service-name=e2e-service-provider
    expected: expected/metrics-has-value.yml
  - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics exec --expression=instance_jvm_thread_live_count --instance-name=provider1 --service-name=e2e-service-provider
    expected: expected/metrics-has-value.yml
  - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics exec --expression=instance_jvm_thread_daemon_count --instance-name=provider1 --service-name=e2e-service-provider
    expected: expected/metrics-has-value.yml
  - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics exec --expression=instance_jvm_thread_peak_count --instance-name=provider1 --service-name=e2e-service-provider
    expected: expected/metrics-has-value.yml
  - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics exec --expression=instance_jvm_thread_runnable_state_thread_count --instance-name=provider1 --service-name=e2e-service-provider
    expected: expected/metrics-has-value.yml
  - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics exec --expression=instance_jvm_class_loaded_class_count --instance-name=provider1 --service-name=e2e-service-provider
    expected: expected/metrics-has-value.yml
  - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics exec --expression=instance_jvm_class_total_loaded_class_count --instance-name=provider1 --service-name=e2e-service-provider
    expected: expected/metrics-has-value.yml
  # service endpoint metrics
  - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics exec --expression=endpoint_cpm --endpoint-name=POST:/users --service-name=e2e-service-provider
    expected: expected/metrics-has-value.yml
  - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics exec --expression=endpoint_resp_time --endpoint-name=POST:/users --service-name=e2e-service-provider
    expected: expected/metrics-has-value.yml
  - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics exec --expression=endpoint_sla --endpoint-name=POST:/users --service-name=e2e-service-provider
    expected: expected/metrics-has-value.yml
  - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics exec --expression=endpoint_cpm --endpoint-name=POST:/users --service-name=e2e-service-consumer
    expected: expected/metrics-has-value.yml
  - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics exec --expression=endpoint_resp_time --endpoint-name=POST:/users --service-name=e2e-service-consumer
    expected: expected/metrics-has-value.yml
  - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics exec --expression=endpoint_sla --endpoint-name=POST:/users --service-name=e2e-service-consumer
    expected: expected/metrics-has-value.yml
  # service endpoint metrics percentile
  - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics exec --expression=endpoint_percentile --endpoint-name=POST:/users --service-name=e2e-service-consumer
    expected: expected/metrics-has-value-percentile.yml
  - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics exec --expression=endpoint_percentile --endpoint-name=POST:/users --service-name=e2e-service-consumer
    expected: expected/metrics-has-value-percentile.yml
  # Endpoint TopN with service_id
  - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics top --name endpoint_cpm --service-name e2e-service-provider 5
    expected: expected/metrics-top-endpoint.yml
  # native event: event list
  - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql event list
    expected: expected/event-list.yml

  - query: |
      curl -s -XPOST http://${provider_host}:${provider_9090}/users -d '{"id":"123","name":"SinglesBar"}' -H "Content-Type: application/json" > /dev/null;
      sleep 5;
      swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql trace $( \
        swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql trace ls --order startTime --service-name "e2e-service-provider" --endpoint-name "POST:/users" \
          | yq e '.traces[0].traceids[0]' - \
      )
    expected: expected/trace-users-detail.yml
